/**
 * 
 */
package com.cqcis.uip.base.framework.core.config;

import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;
import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.Properties;

/**
 * @author huangmy
 * @date 2008-2-18
 */
public class PropertiesConfig {
	private long lastModified; /* 配置文件最后修改时间 */
	private Map<String, String> properties; /* 配置文件内容 */
	private String filename;
	
	private PropertiesConfig(long lastModified, Map<String, String> properties, String filename) {
		this.lastModified = lastModified;
		this.properties = properties;
		this.filename = filename;
	}
	
	public static PropertiesConfig getPropertiesConfig(File file) throws Exception {
		final Map<String, String> properties = new LinkedHashMap<String, String>();
		Properties pp = new Properties() {			
			private static final long serialVersionUID = -7026278573225656388L;

			@Override
			public synchronized Object put(Object key, Object value) {				
				return properties.put((String)key, (String)value);
			}
		};
		
		if (!file.getName().endsWith(".properties")) {
			return new PropertiesConfig(file.lastModified(), properties, file.getName());
		}
		
		InputStream is = null;		
		try {
			is = new FileInputStream(file);
			pp.load(is);
		} finally {
			if (is != null) {
				is.close();
			}
		}
		return new PropertiesConfig(file.lastModified(), properties, file.getName());
	}
	
	public boolean equalsWith(File file) {
		if (getLastModified() < file.lastModified()) {
			return false;
		}
		
		PropertiesConfig newConfig = null;
		try {
			newConfig = getPropertiesConfig(file);
		} catch (Exception e) {
			return false;
		}
		
		if (file.getName().endsWith(".properties") && !equals(newConfig)) {
			return false;
		}
		
		return true;
	}
	
	public long getLastModified() {
		return lastModified;
	}

	public void setLastModified(long lastModified) {
		this.lastModified = lastModified;
	}

	public Map<String, String> getProperties() {
		return properties;
	}

	public void setProperties(Map<String, String> properties) {
		this.properties = properties;
	}
	
	@Override
	public boolean equals(Object pc) {
		if (pc == null) {
			return false;
		}
		
		PropertiesConfig other = (PropertiesConfig)pc;
		
		//最后修改时间
		if (!this.filename.endsWith(".properties")) {
			if (getLastModified() != other.getLastModified()) {
				return false;
			}			
		}
				
		//属性
		if (getProperties().size() != other.getProperties().size()) {
			return false;
		}
				
		for (Iterator<String> it = getProperties().keySet().iterator(); it.hasNext();) {
			String name = it.next();
			
			String selfValue = getProperties().get(name);
			String otherValue = other.getProperties().get(name);
			
			if ((selfValue != null && otherValue == null) 
					|| (selfValue == null && otherValue != null)) {
				return false;
			}
			
			if (selfValue != null && otherValue != null) {
				if (!selfValue.equals(otherValue)) {					
					return false;
				}
			}
		}
		
		return true;
	}
}
